package com.timothy.android;

import java.util.ArrayList;
import java.util.List;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.VerticalBar2D;
import com.timothy.question.TypeBean1;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class Bar2DAnswerActivity extends Activity {
	private final static String LOG_TAG = "Bar2DAnswerActivity";
	private TypeBean1 bean;
	List<VerticalBar2D> barList;
	private int totalVotes = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		setContentView(new MyView(this));

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(new VerticalBar2DView(this));
		
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//		setContentView(new HorizontalBar2DView(this));
		
		bean = TypeBean1.getMockInstance();
		
		//获取图形数据
		barList = new ArrayList<VerticalBar2D>();
		VerticalBar2D bar1=new VerticalBar2D(0,50,"A",1,Color.RED);
		totalVotes+=bar1.getVotes();
		barList.add(bar1);
		VerticalBar2D bar2=new VerticalBar2D(1,50,"B",1,Color.GREEN);
		totalVotes+=bar2.getVotes();
		barList.add(bar2);
		VerticalBar2D bar3=new VerticalBar2D(2,800,"C",1,Color.YELLOW);
		totalVotes+=bar3.getVotes();
		barList.add(bar3);
		VerticalBar2D bar4=new VerticalBar2D(3,10,"D",1,Color.BLUE);
		totalVotes+=bar4.getVotes();
		barList.add(bar4);
		VerticalBar2D bar5=new VerticalBar2D(4,10,"E",1,Color.CYAN);
		totalVotes+=bar5.getVotes();
		barList.add(bar5);
		VerticalBar2D bar6=new VerticalBar2D(5,80,"F",1,Color.LTGRAY);
		totalVotes+=bar6.getVotes();
		barList.add(bar6);
		
		Log.i(LOG_TAG, "VerticalBar2DView:totalVotes:"+totalVotes);
	}
	
	class VerticalBar2DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public VerticalBar2DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalBar2DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalBar2DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			
			//write question
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			Paint mPaint = new Paint();
			mPaint.setColor(Color.WHITE);		
			mPaint.setTextSize(25);
			canvas.drawText(bean.getQuestion(), 50, 30, mPaint); 
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "VerticalBar2DView:spaceInCharts:"+setting.getSpaceInCylis());
			int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
			double barWidthInclSpace = (canvasNoMarginWidth * 1.0)/barList.size();
			int barWidth = (int)(barWidthInclSpace*0.9);
			setting.setSpaceInCylis((int)(barWidthInclSpace-barWidth));
			Log.i(LOG_TAG, "VerticalBar2DView:barWidth:"+barWidth);
			
			for(int i=0;i<barList.size();i++){
				VerticalBar2D bar2D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				
				int votes = bar2D.getVotes();
				double votesShare = (1.0* bar2D.getVotes())/totalVotes;
				int barHeigth = (int) (votesShare * canvasHeight);
				
				Log.i(LOG_TAG, "VerticalBar2DView:barHeigth:"+barHeigth);
				Log.i(LOG_TAG, "VerticalBar2DView:votes:"+votes);
				Log.i(LOG_TAG, "VerticalBar2DView:votesShare:"+votesShare);
				
				String answerText = null; 
				switch (bar2D.getAnswerType()){
				    case 0:answerText =  String.valueOf(votes);break; 
				    case 1:answerText =  String.valueOf(votesShare*100)+"%";break;
				    case 2:answerText =  String.valueOf(votes)+":"+String.valueOf(votesShare*100)+"%";break;
				}
				
				Log.i(LOG_TAG, "VerticalBar2DView:answerText:"+answerText);
				
				int tlX = (int) (setting.getLeftMargin()+ i * (barWidth + setting.getSpaceInCylis()));
				int tlY =  canvasHeight - setting.getBottomMargin() - barHeigth;
				int brX = tlX + barWidth;
				int brY = canvasHeight - setting.getBottomMargin();
				
				Log.i(LOG_TAG, "VerticalBar2DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				

				mPaint.setColor(bar2D.getFullColor());
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				canvas.save();
				
				mPaint.setTextSize(20);
				canvas.drawText(answerText, tlX, tlY-20, mPaint);
				canvas.drawText(bar2D.getChoiceText(), tlX, canvasHeight-20, mPaint);
				canvas.save();
			}

//			canvas.save();
			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}
	
	class HorizontalBar2DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public HorizontalBar2DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "HorizontalBar2DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "HorizontalBar2DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,2);
			
			//write question
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			Paint mPaint = new Paint();
			mPaint.setColor(Color.WHITE);		
			mPaint.setTextSize(25);
			canvas.drawText(bean.getQuestion(),canvasWidth/4,30, mPaint); 
			canvas.save();
			
			//根据图形个数既图形间距计算出图形宽度
			Log.i(LOG_TAG, "HorizontalBar2DView:spaceInCharts:"+setting.getSpaceInCylis());
//			int barWidth = (canvasWidth - setting.getLeftMargin() - setting.getRightMargin())/barList.size() - (int) setting.getSpaceInCylis();
			int barHeight = (canvasHeight - setting.getTopMargin() - setting.getBottomMargin())/barList.size() - (int) setting.getSpaceInCylis();
			Log.i(LOG_TAG, "HorizontalBar2DView:barHeight:"+barHeight);
			
			for(int i=0;i<barList.size();i++){
				VerticalBar2D bar2D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				
				int votes = bar2D.getVotes();
				double votesShare = (1.0* bar2D.getVotes())/totalVotes;
//				int barHeigth = (int) (votesShare * canvasHeight);
				int barWidth = (int) (votesShare * canvasWidth);
				
				Log.i(LOG_TAG, "HorizontalBar2DView:barWidth:"+barWidth);
				Log.i(LOG_TAG, "HorizontalBar2DView:votes:"+votes);
				Log.i(LOG_TAG, "HorizontalBar2DView:votesShare:"+votesShare);
				
				String answerText = null; 
				switch (bar2D.getAnswerType()){
				    case 0:answerText =  String.valueOf(votes);break; 
				    case 1:answerText =  String.valueOf(votesShare*100)+"%";break;
				    case 2:answerText =  String.valueOf(votes)+":"+String.valueOf(votesShare*100)+"%";break;
				}
				
				Log.i(LOG_TAG, "HorizontalBar2DView:answerText:"+answerText);
				
				int tlX = setting.getLeftMargin();
				int tlY = setting.getTopMargin()+ (int)(i*(barHeight + setting.getSpaceInCylis()));
				int brX = tlX + barWidth;
				int brY = tlY + barHeight;
				
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				
				mPaint.setTextSize(25);
				canvas.drawText(answerText, tlX + barWidth+10, tlY+barHeight/2, mPaint);
				
				canvas.drawText(bar2D.getChoiceText(),10, tlY+barHeight/2, mPaint);
				
				mPaint.setColor(bar2D.getFullColor());
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				
				canvas.save();
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}
	
	
	
}
